<?php
require_once 'DBConnecter.inc';
class Login extends DBConnecter {
	public $mbEid;
	public $mbPasswd;
	public $dt;
	
	function __construct($eid, $passwd) {
		$this->mbEid = $eid;
		$this->mbPasswd = $passwd;
		$this->connectDB ();
		// $this->selectDB ( 'devdog' );
	}
	public function confirmInfo($eid, $passwd) {
		$query = "select ".self::CONST_DB.".login_chk('" . $eid . "','" . $passwd . "')";
		$result = $this->query ( $query );
		
		if (! empty ( $result )) {
			$row = mysqli_fetch_row ( $result );
			if (! empty ( $row )) {
				switch ($row [0]) {
					case "1" : // 최고 관리자
					case "2" : // 일반 관리자
					case "3" : // 회원
					case "4" : // 업체
						$_SESSION ['memberGrade'] = $row [0];
						return $_SESSION ['memberGrade'];
				}
			}
			$result->free ();
		}
		
		echo "<script>alert(\"사원번호 또는 비밀번호를 다시 확인해주세요.\"); window.location.replace('".self::CONST_HOME."');</script>";
		exit ();
	}
	public function setSession($eid, $passwd) {
		if (! isset ( $_SESSION ['eidSession'] )) {
			$_SESSION ['eidSession'] = $eid; // id 값 쓸일이 있기 때문에 패스워드와 조합하지 않음.
		}
	}
	public function getSession() {
		if (isset ( $_SESSION ['eidSession'] )) {
			return $_SESSION ['eidSession'];
		} else {
			return null;
		}
	}
	public function setSessionTime() {
		if (! isset ( $_SESSION ['time'] )) {
			$_SESSION ['time'] = time ();
		}
	}
	public function updateSessionTime() {
		$_SESSION ['time'] = time ();
	}
	public function getSessionTime() {
		if (isset ( $_SESSION ['time'] )) {
			return $_SESSION ['time'];
		} else {
			return null;
		}
	}
	public function destroySession() {
		if (isset ( $_SESSION ['eidSession'] )) {
			unset ( $_SESSION ['eidSession'] );
		}
		if (isset ( $_SESSION ['time'] )) {
			unset ( $_SESSION ['time'] );
		}
		if (isset ( $_SESSION ['memberGrade'] )) {
			unset ( $_SESSION ['memberGrade'] );
		}
		session_destroy ();
		$this->closeDB ();
	}
	public function getGrade() {
		if (isset ( $_SESSION ['memberGrade'] )) {
			return $_SESSION ['memberGrade'];
		} else {
			return - 1;
		}
	}
	public function checkDailyWorkCondition() {
		$query = "select ".self::CONST_DB.".daily_account_chk()";
		$result = $this->query ( $query );
		if (! empty ( $result )) {
			$row = mysqli_fetch_row ( $result );
			if (! empty ( $row ) && $row [0] == "0") {
				$sql = "CALL ".self::CONST_DB.".daily_accounts('1','0','0','0','0','0','0','0','0','', '');select @out_err_cd, @out_err_msg;";
				if ($rs = $this->getMySqlConneter ()->multi_query ( $sql )) {
					if ($rs != null && $rs = $this->getMySqlConneter ()->use_result ()) {
						while ( $row = $rs->fetch_row () ) {
							$dt = $row ['0'];
						}
						$result->free ();
					}
				}
			}	
		}
	}
}
?>